#!/bin/bash


export PATH=/usr/bin:/usr/sbin:/bin:/sbin


verify_packages()
{

GCC_PACKAGES="
ppl-0.10.2-11.el6.x86_64
glibc-headers-2.12-1.107.el6.x86_64
libgcc-4.4.7-3.el6.x86_64
mpfr-2.4.1-6.el6.x86_64
kernel-headers-2.6.32-358.el6.x86_64
glibc-devel-2.12-1.107.el6.x86_64
gcc-4.4.7-3.el6.x86_64
cpp-4.4.7-3.el6.x86_64
cloog-ppl-0.15.7-1.2.el6.x86_64
"

printf "%-60s\n" "Verifying GCC and dependencies are installed..."

for package in $(echo $GCC_PACKAGES); do
	if rpm -q --quiet $package; then 
		printf "	%-40s installed\n" $package
	else
		printf "	%-40s missing\n" $package
	fi
done

}

customize_env()
{
# adding /opt/bind/sbin:/opt/bind/bin to PATH
printf "%-60s" "Creating /etc/profile.d/bind.sh..."
if [ -f /etc/profile.d/bind.sh ]; then
	echo "Skipped"
else
	echo -e ' 
BINDDIR="/opt/bind"

if ! echo ${PATH} | /bin/grep -q "${BINDDIR}/sbin"; then
                PATH=${BINDDIR}/sbin:${BINDDIR}/bin:${PATH}
                export PATH
fi
' > /etc/profile.d/bind.sh

	if [ $? -eq 0 ]; then echo "Done"; else echo "Failed"; fi
fi

# adding /opt/bind/share/man to MANPATH
 
printf "%-60s" "Modifying /etc/man.config to add BIND to MANPATH..."
if ! grep -q "^MANPATH[[:space:]]\{1,\}/opt/bind/share/man[[:space:]]*$" /etc/man.config; then
	cp -a /etc/man.config /etc/man.config.$(/bin/date "+%C%y%m%d%H%M%S")
	sed -e "s/^MANPATH[[:space:]]\{1,\}\/usr\/man[[:space:]]*$/MANPATH	\/opt\/bind\/share\/man\nMANPATH	\/usr\/man/" /etc/man.config > $TMPFILE
	sed -e "s/^MANPATH_MAP[[:space:]]\{1,\}\/bin[[:space:]]\{1,\}\/usr\/share\/man[[:space:]]*$/MANPATH_MAP	\/opt\/bind\/bin		\/opt\/bind\/share\/man\nMANPATH_MAP	\/opt\/bind\/sbin		\/opt\/bind\/share\/man\nMANPATH_MAP	\/bin			\/usr\/share\/man/" $TMPFILE > /etc/man.config
	echo "Done"
else
	echo "Skipped"
fi

printf "%-60s" "Modifying /etc/updatedb.conf to exclude /var/named..."
grep "^PRUNEPATHS" /etc/updatedb.conf | grep  -q -E "[\"| ]/var/named[\"| ]"
if [ $? -eq 0 ]; then
	echo "Skipped"
else
	sed -e "s/\/var\/tmp/\/var\/tmp \/var\/named/" /etc/updatedb.conf > $TMPFILE
	cp $TMPFILE /etc/updatedb.conf
	
	if [ $? -eq 0 -a -s /etc/updatedb.conf ]; then echo "Succeeded"; else echo "Failed"; fi
fi

# create a symblic link /bin/perl -> /usr/bin/perl
printf "%-60s" "Creating a a symblic link /bin/perl..."
if [ -L /bin/perl ]; then
	echo "Skipped"
else
	ln -s /usr/bin/perl /bin/perl
	if [ $? -eq 0 ]; then echo "Succeeded"; else echo "Failed"; fi
fi

# allow root login from master DNS servers
printf "%-60s" "Adding SSH key for root..."
grep -q '^ssh-dss AAAAB3NzaC1kc3MAAACBANXNZKyitH2i97lkYexvoVSYtrtYMQBGhrSUcnD6vw8Uam66VWW+FS7kQY/sBaAuJRnCg1bOjqudAIoiYrd7JbMNfNkvDUcoBvbARer6SA7wTkzDr8WECgiGYAPtom3rIy/ruNs1t7t3mWCLwrTJku+D6hAUOM723/4D3MtTnVYjAAAAFQC21yuxc40Ya9sa3Mz5LDMb35cFjQAAAIEAxnW+fyCfNeIGX43iRY9a3steGTjGfJFCIKdiC8QvBKEvo+sW4fbtQVLDZDTxevn/TKUQfa+BcQpLUeXayDBVTwbj1yQzijD0j+tApKI8987UDJ6OdP3P5pJDtrP8Ycj4pjtUdmiW/sVKx5wnCqzhZch2aFGrIt5spDOOriHLySQAAACACe9wJRuOLaIe9eCB9/mv+6zOElQBcpi3ihYpEcwOXAZWZco0AVt6FcMyldfLwlwAhLCeugZ1Q4al+WmtGyeWctRolc7/PFS37t+soXLVKZZqjSWxIlnjUGhmYkIQDjGQWGEVF6CcfiLBdsa1MEBmvvsc3TjFX0swUIy2ES27eok= root@sjcitdns01$' /root/.ssh/authorized_keys
if [ $? -eq 0 ]; then echo "Skipped"
else 
        echo -e 'ssh-dss AAAAB3NzaC1kc3MAAACBANXNZKyitH2i97lkYexvoVSYtrtYMQBGhrSUcnD6vw8Uam66VWW+FS7kQY/sBaAuJRnCg1bOjqudAIoiYrd7JbMNfNkvDUcoBvbARer6SA7wTkzDr8WECgiGYAPtom3rIy/ruNs1t7t3mWCLwrTJku+D6hAUOM723/4D3MtTnVYjAAAAFQC21yuxc40Ya9sa3Mz5LDMb35cFjQAAAIEAxnW+fyCfNeIGX43iRY9a3steGTjGfJFCIKdiC8QvBKEvo+sW4fbtQVLDZDTxevn/TKUQfa+BcQpLUeXayDBVTwbj1yQzijD0j+tApKI8987UDJ6OdP3P5pJDtrP8Ycj4pjtUdmiW/sVKx5wnCqzhZch2aFGrIt5spDOOriHLySQAAACACe9wJRuOLaIe9eCB9/mv+6zOElQBcpi3ihYpEcwOXAZWZco0AVt6FcMyldfLwlwAhLCeugZ1Q4al+WmtGyeWctRolc7/PFS37t+soXLVKZZqjSWxIlnjUGhmYkIQDjGQWGEVF6CcfiLBdsa1MEBmvvsc3TjFX0swUIy2ES27eok= root@sjcitdns01' >> /root/.ssh/authorized_keys
        if [ $? -eq 0 ] ;then echo "Succeeded"; else echo "Failed"; fi
fi

}

create_user()
{
#set -x	
	if id -u named >/dev/null 2>&1; then
		printf "%-60s" "Verifying user \"named\"..."
		if grep -q "^named:x:25:25:.*:/var/named:/sbin/nologin$"	/etc/passwd; then
			echo "Passed"
		else
			echo "Failed"
		fi
		printf "%-60s" "Verifying password aging of \"named\"..."
		if passwd -S named | grep -q "^named LK .* 0 99999 [0-9]\{1,\} -1"; then
			echo "Passed"
		else
			echo "Failed"
		fi
	else
		if ! grep -q "^named:25:" /etc/group; then
			printf "%-60s" "Creating group \"named\"..."
			groupadd -g 25 named >/dev/null 2>&1
			if [ $? -eq 0 ]; then echo "Succeeded"; else echo  "Failed"; fi
		fi
		printf "%-60s" "Creating user \"named\"..."
		useradd -u 25 -g 25 -d /var/named -c "Named" -s /sbin/nologin named >/dev/null 2>&1
		if [ $? -eq 0 ]; then echo "Succeeded"; else echo  "Failed"; fi
		passwd -n 0 -x 99999 -w 7 -i -1 named >/dev/null 2>&1
	
	fi
		
}

create_direcotries()
{
printf "%-60s" "Creating directories for configuration, DBs, logs, etc..."
dbs="root base its mp pp misc qa bl nodb"

errors="no"
for d in /etc/named /ebay/bind-logs /var/named/db /var/named/tmp /var/named/run; do
        if [ ! -d $d ]; then errors="yes"; fi
done

if [ -d /var/named/db ]; then 
        cd /var/named/db
        for d in $(echo $dbs); do if [ ! -d $d ]; then errors="yes"; fi; done
else    
        errors="yes"
fi

if [ $errors == "no" ]; then
        echo "Skipped"
else
        errors="no"
        mkdir -p /etc/named /ebay/bind-logs /var/named/db /var/named/tmp /var/named/run 2>/dev/null
        if [ $? -ne 0 ]; then errors="yes"; fi
        for d in $(echo $dbs); do 
                mkdir -p /var/named/db/${d} 2>/dev/null
                if [ $? -ne 0 ]; then errors="yes"; fi
        done
        chown -R named:named /etc/named /var/named /ebay/bind-logs 2>/dev/null
        if [ $? -ne 0 ]; then errors="yes"; fi
        if [ $errors == "no" ] ;then echo "Succeeded"; else echo "Failed"; fi
fi
}

configure_service()
{
printf "%-60s" "Configuring named in services..."
if chkconfig --list named 2>/dev/null | grep -q "^named[[:space:]]"; then
	echo "Skipped"
else
	if [ -f /usr/local/sbin/rhel6.named.init ]; then
		cp /usr/local/sbin/rhel6.named.init /etc/init.d/named
		chmod 755  /etc/init.d/named
		chkconfig --add named
		if [ $? -eq 0 ]; then echo "Succeeded"; else echo "Failed"; fi
	else
		echo "Failed"
	fi

fi
}

### MAIN ###

TMPFILE=/var/tmp/$$.tmp

verify_packages
customize_env
create_user
create_direcotries
configure_service

rm -f $TMPFILE
exit 0
